﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PRET.ReportDataDefine.DBData
{
    /// <summary>
    /// 挤出机数据——对应DB中的JCJInfo
    /// </summary>
    public class JCJData
    {
        public string ProductionLine { get; set; }
        public string ProLineCode { get; set; }
        public string OrderID { get; set; }
        public DateTime? StartTime { get; set; }
        public DateTime? EndTime { get; set; }
        public DateTime UpdateTime { get; set; }
        //public string Bal1MaterialName { get; set; }
        //public string Bal1BatchID { get; set; }
        public float Bal1Scale { get; set; }
        //public string Bal2MaterialName { get; set; }
        //public string Bal2BatchID { get; set; }
        public float Bal2Scale { get; set; }
        //public string Bal3MaterialName { get; set; }
        //public string Bal3BatchID { get; set; }
        public float Bal3Scale { get; set; }
        public float JCJ1TempOffset { get; set; }
        public float JCJ2TempOffset { get; set; }
        public float JCJ3TempOffset { get; set; }
        public float JCJ4TempOffset { get; set; }
        public float JCJ5TempOffset { get; set; }
        public float JCJ6TempOffset { get; set; }
        public float JCJ7TempOffset { get; set; }
        public float JCJ8TempOffset { get; set; }
        public float JCJ9TempOffset { get; set; }
        public float JCJ10TempOffset { get; set; }
        public float JCJ1SetTemp { get; set; }
        public float JCJ2SetTemp { get; set; }
        public float JCJ3SetTemp { get; set; }
        public float JCJ4SetTemp { get; set; }
        public float JCJ5SetTemp { get; set; }
        public float JCJ6SetTemp { get; set; }
        public float JCJ7SetTemp { get; set; }
        public float JCJ8SetTemp { get; set; }
        public float JCJ9SetTemp { get; set; }
        public float JCJ10SetTemp { get; set; }
        public int ExtruderMaxRevolvingSpeed { get; set; }
        public int ExtruderMinRevolvingSpeed { get; set; }
        public float ExtruderMaxCurrent { get; set; }
        public float ExtruderMinCurrent { get; set; }
        public float MaxRongZhi { get; set; }
        public float MinRongZhi { get; set; }
        public float MaxRemnant { get; set; }
        public float MinRemnant { get; set; }
        public float MaxCurrent { get; set; }
        public float MaxPower { get; set; }
        public float MinCurrent { get; set; }
        public double StartPower { get; set; }
        public double EndPower { get; set; }
        public List<JCJDetail> Detail { get; set; }

        private readonly static int HalfHour = 1800;

        public static JCJData ConvertDictionary2JCJData(Dictionary<string, string> dic, string proLineCode)
        {
            if (dic == null || dic.Count == 0)
                return null;

            JCJData obj = new JCJData();
            obj.ProductionLine = IDConvert.ProLineConvert(dic["ProLine1"], dic["ProLine2"]);
            obj.ProLineCode = proLineCode;
            obj.OrderID = IDConvert.OrderIDConvert(dic["OrderNum4"], dic["OrderNum3"], dic["OrderNum2"], dic["OrderNum1"]);
            if (!string.IsNullOrEmpty(dic["Time"]))
            {
                obj.EndTime = DateTime.Parse(dic["Time"]);
                obj.UpdateTime = DateTime.Parse(dic["Time"]);
            }
            if (dic.Keys.Contains("degreeH") && dic.Keys.Contains("degreeL"))
                obj.EndPower = double.Parse(dic["degreeH"]) * 65535 + double.Parse(dic["degreeL"]);
            obj.MaxCurrent = float.MinValue;
            obj.MinCurrent = float.MaxValue;
            obj.MaxPower = float.MinValue;

            return obj;
        }
        public static JCJData ConvertDictionary2JCJData(Dictionary<string, List<string>> dic, string proLineCode)
        {
            if (dic == null || dic.Count == 0)
                return null;
            JCJData obj = new JCJData();
            obj.ProductionLine = IDConvert.ProLineConvert(dic["ProLine1"][0], dic["ProLine2"][0]);
            obj.ProLineCode = proLineCode;
            obj.OrderID = IDConvert.OrderIDConvert(dic["OrderNum4"][0], dic["OrderNum3"][0], dic["OrderNum2"][0], dic["OrderNum1"][0]);
            obj.StartTime = DateTime.Parse(dic["Time"][0]);
            obj.EndTime = null;
            obj.UpdateTime = DateTime.Parse(dic["Time"][dic["Time"].Count - 1]);
            //obj.Weight = float.Parse(dic["Count"][0]);
            //obj.Bal1MaterialName = IDConvert.BalIDConvert(dic["BalName1_1"][0], dic["BalName1_2"][0], dic["BalName1_3"][0], dic["BalName1_4"][0]);
            //obj.Bal1BatchID = IDConvert.BalIDConvert(dic["BalBatNum1_1"][0], dic["BalBatNum1_2"][0], dic["BalBatNum1_3"][0], dic["BalBatNum1_4"][0]);
            //obj.Bal2MaterialName = IDConvert.BalIDConvert(dic["BalName2_1"][0], dic["BalName2_2"][0], dic["BalName2_3"][0], dic["BalName2_4"][0]);
            //obj.Bal2BatchID = IDConvert.BalIDConvert(dic["BalBatNum2_1"][0], dic["BalBatNum2_2"][0], dic["BalBatNum2_3"][0], dic["BalBatNum2_4"][0]);
            //obj.Bal3MaterialName = IDConvert.BalIDConvert(dic["BalName3_1"][0], dic["BalName3_2"][0], dic["BalName3_3"][0], dic["BalName3_4"][0]);
            //obj.Bal3BatchID = IDConvert.BalIDConvert(dic["BalBatNum3_1"][0], dic["BalBatNum3_2"][0], dic["BalBatNum3_3"][0], dic["BalBatNum3_4"][0]);
            obj.Bal1Scale = float.Parse(dic["BalScale1"][0]) / 1000;
            obj.Bal2Scale = float.Parse(dic["BalScale2"][0]) / 1000;
            obj.Bal3Scale = float.Parse(dic["BalScale3"][0]) / 1000;
            obj.JCJ1TempOffset = float.Parse(dic["JTemOff1"][0]);
            obj.JCJ1SetTemp = float.Parse(dic["JTemSet1"][0]);
            obj.JCJ2TempOffset = float.Parse(dic["JTemOff2"][0]);
            obj.JCJ2SetTemp = float.Parse(dic["JTemSet2"][0]);
            obj.JCJ3TempOffset = float.Parse(dic["JTemOff3"][0]);
            obj.JCJ3SetTemp = float.Parse(dic["JTemSet3"][0]);
            obj.JCJ4TempOffset = float.Parse(dic["JTemOff4"][0]);
            obj.JCJ4SetTemp = float.Parse(dic["JTemSet4"][0]);
            obj.JCJ5TempOffset = float.Parse(dic["JTemOff5"][0]);
            obj.JCJ5SetTemp = float.Parse(dic["JTemSet5"][0]);
            obj.JCJ6TempOffset = float.Parse(dic["JTemOff6"][0]);
            obj.JCJ6SetTemp = float.Parse(dic["JTemSet6"][0]);
            obj.JCJ7TempOffset = float.Parse(dic["JTemOff7"][0]);
            obj.JCJ7SetTemp = float.Parse(dic["JTemSet7"][0]);
            obj.JCJ8TempOffset = float.Parse(dic["JTemOff8"][0]);
            obj.JCJ8SetTemp = float.Parse(dic["JTemSet8"][0]);
            obj.JCJ9TempOffset = float.Parse(dic["JTemOff9"][0]);
            obj.JCJ9SetTemp = float.Parse(dic["JTemSet9"][0]);
            obj.JCJ10TempOffset = float.Parse(dic["JTemOff10"][0]);
            obj.JCJ10SetTemp = float.Parse(dic["JTemSet10"][0]);
            obj.ExtruderMaxRevolvingSpeed = int.Parse(dic["JMaxRev"][0]);
            obj.ExtruderMinRevolvingSpeed = int.Parse(dic["JMinRev"][0]);
            obj.ExtruderMaxCurrent = float.Parse(dic["JMaxCur"][0]);
            obj.ExtruderMinCurrent = float.Parse(dic["JMinCur"][0]);
            obj.MaxRongZhi = float.Parse(dic["MaxRZ"][0]);
            obj.MinRongZhi = float.Parse(dic["MinRZ"][0]);
            obj.MaxRemnant = float.Parse(dic["MaxRemn"][0]);
            obj.MinRemnant = float.Parse(dic["MinRemn"][0]);
            if (dic.Keys.Contains("degreeH") && dic.Keys.Contains("degreeL"))
            {
                obj.StartPower = double.Parse(dic["degreeH"][0]) * 65535 + double.Parse(dic["degreeL"][0]);
                int count = dic["degreeH"].Count - 1;
                obj.EndPower = double.Parse(dic["degreeH"][count]) * 65535 + double.Parse(dic["degreeL"][count]);
            }
            float maxCurrent, minCurrent, maxPower;
            obj.Detail = Convert2JCJDetail(dic, proLineCode, out maxCurrent, out minCurrent, out maxPower);
            obj.MaxCurrent = maxCurrent;
            obj.MinCurrent = minCurrent;
            obj.MaxPower = maxPower;
            return obj;
        }
        private static List<JCJDetail> Convert2JCJDetail(Dictionary<string, List<string>> dic, string proLineCode, out float maxCurrent, out float minCurrent, out float maxPower)
        {
            maxCurrent = int.MinValue;
            minCurrent = int.MaxValue;
            maxPower = int.MinValue;
            if (dic == null || dic.Count == 0)
            {
                return null;
            }

            List<JCJDetail> list = new List<JCJDetail>();
            int count = dic["Time"].Count;
            for (int i = 0; i < count; i++ )
            {
                JCJDetail obj = new JCJDetail();
                obj.ProductionLine = IDConvert.ProLineConvert(dic["ProLine1"][i], dic["ProLine2"][i]); ;
                obj.OrderID = IDConvert.OrderIDConvert(dic["OrderNum4"][i], dic["OrderNum3"][i], dic["OrderNum2"][i], dic["OrderNum1"][i]);
                obj.Bal1RealCount = float.Parse(dic["BalRCount1"][i]);
                obj.Bal2RealCount = float.Parse(dic["BalRCount2"][i]);
                obj.Bal3RealCount = float.Parse(dic["BalRCount3"][i]);
                obj.WLJRevolvingSpeed = int.Parse(dic["WLJRev"][i]);
                obj.JCJ1RealTemp = float.Parse(dic["JTemReal1"][i]);
                obj.JCJ2RealTemp = float.Parse(dic["JTemReal2"][i]);
                obj.JCJ3RealTemp = float.Parse(dic["JTemReal3"][i]);
                obj.JCJ4RealTemp = float.Parse(dic["JTemReal4"][i]);
                obj.JCJ5RealTemp = float.Parse(dic["JTemReal5"][i]);
                obj.JCJ6RealTemp = float.Parse(dic["JTemReal6"][i]);
                obj.JCJ7RealTemp = float.Parse(dic["JTemReal7"][i]);
                obj.JCJ8RealTemp = float.Parse(dic["JTemReal8"][i]);
                obj.JCJ9RealTemp = float.Parse(dic["JTemReal9"][i]);
                obj.JCJ10RealTemp = float.Parse(dic["JTemReal10"][i]);
                obj.ExtruderRevolvingRealSpeed = int.Parse(dic["JRealRev"][i]);
                obj.ExtruderRealCurrent = float.Parse(dic["JRealCur"][i]) / 10;
                obj.RealRongZhi = float.Parse(dic["RealRz"][i]);
                obj.RealRemnant = float.Parse(dic["RealRemn"][i]);
                obj.RecordTime = DateTime.Parse(dic["Time"][i]);
                obj.UpdateTime = DateTime.Parse(dic["Time"][i]);
                obj.ProLineCode = proLineCode;
                float Ia = float.Parse(dic["Ia"][i]);
                float Ib = float.Parse(dic["Ib"][i]);
                float Ic = float.Parse(dic["Ic"][i]);
                float ele = (Ia + Ib + Ic) / 3;
                maxCurrent = maxCurrent < ele ? ele : maxCurrent;
                minCurrent = minCurrent > ele ? ele : minCurrent;
                float power = float.Parse(dic["power"][i]);
                maxPower = maxPower < power ? power : maxPower;
                list.Add(obj);
            }

            List<JCJDetail> finalList = new List<JCJDetail>();
            // 以1800秒（30分钟）为间隔来整合挤出机数据
            DateTime nextTime = list[0].RecordTime.AddSeconds(HalfHour);
            List<JCJDetail> tmpList = new List<JCJDetail>();
            foreach (JCJDetail item in list)
            {
                tmpList.Add(item);
                // 以30分钟为间隔来整合挤出机数据
                if (DateTime.Compare(item.RecordTime, nextTime) >= 0)
                {
                    finalList.Add(JCJData.MergerJCJDataList(tmpList));
                    tmpList.Clear();
                    nextTime = item.RecordTime.AddSeconds(HalfHour);
                }
            }
            if (tmpList.Count > 0 && finalList.Count == 0)
                finalList.Add(JCJData.MergerJCJDataList(tmpList));

            if (maxCurrent == int.MinValue)
                maxCurrent = 0;
            if (minCurrent == int.MaxValue)
                minCurrent = 0;
            if (maxPower == int.MinValue)
                maxPower = 0;
            return finalList;
        }
        private static JCJDetail MergerJCJDataList(List<JCJDetail> list)
        {
            if (list == null || list.Count == 0)
                return null;
            else if (list.Count == 1)
                return list[0];

            int count = list.Count;
            JCJDetail obj = new JCJDetail();
            obj.ProductionLine = list[0].ProductionLine;
            obj.ProLineCode = list[0].ProLineCode;
            obj.OrderID = list[0].OrderID;
            obj.RecordTime = list[count - 1].RecordTime;
            obj.UpdateTime = list[count - 1].UpdateTime;
            float Bal1RealCount = 0.0f, Bal2RealCount = 0.0f, Bal3RealCount = 0.0f;
            float JCJ1RealTemp = 0.0f, JCJ2RealTemp = 0.0f, JCJ3RealTemp = 0.0f, JCJ4RealTemp = 0.0f, JCJ5RealTemp = 0.0f, JCJ6RealTemp = 0.0f, JCJ7RealTemp = 0.0f, JCJ8RealTemp = 0.0f, JCJ9RealTemp = 0.0f, JCJ10RealTemp = 0.0f;
            int WLJRevolvingSpeed = 0, ExtruderRevolvingRealSpeed = 0;
            float ExtruderRealCurrent = 0.0f, RealRongZhi = 0.0f, RealRemnant = 0.0f;
            for (int i = 0; i < count; i++)
            {
                WLJRevolvingSpeed += list[i].WLJRevolvingSpeed;
                JCJ1RealTemp += list[i].JCJ1RealTemp;
                JCJ2RealTemp += list[i].JCJ2RealTemp;
                JCJ3RealTemp += list[i].JCJ3RealTemp;
                JCJ4RealTemp += list[i].JCJ4RealTemp;
                JCJ5RealTemp += list[i].JCJ5RealTemp;
                JCJ6RealTemp += list[i].JCJ6RealTemp;
                JCJ7RealTemp += list[i].JCJ7RealTemp;
                JCJ8RealTemp += list[i].JCJ8RealTemp;
                JCJ9RealTemp += list[i].JCJ9RealTemp;
                JCJ10RealTemp += list[i].JCJ10RealTemp;
                Bal3RealCount += list[i].Bal3RealCount;
                Bal1RealCount += list[i].Bal1RealCount;
                Bal2RealCount += list[i].Bal2RealCount;
                Bal3RealCount += list[i].Bal3RealCount;
                ExtruderRevolvingRealSpeed += list[i].ExtruderRevolvingRealSpeed;
                ExtruderRealCurrent += list[i].ExtruderRealCurrent;
                RealRongZhi += list[i].RealRongZhi;
                RealRemnant += list[i].RealRemnant;
            }
            obj.Bal1RealCount = list[count - 1].Bal1RealCount;
            obj.Bal2RealCount = list[count - 1].Bal2RealCount;
            obj.Bal3RealCount = list[count - 1].Bal3RealCount;
            obj.WLJRevolvingSpeed = WLJRevolvingSpeed / count;
            obj.JCJ1RealTemp = JCJ1RealTemp / count;
            obj.JCJ2RealTemp = JCJ2RealTemp / count;
            obj.JCJ3RealTemp = JCJ3RealTemp / count;
            obj.JCJ4RealTemp = JCJ4RealTemp / count;
            obj.JCJ5RealTemp = JCJ5RealTemp / count;
            obj.JCJ6RealTemp = JCJ6RealTemp / count;
            obj.JCJ7RealTemp = JCJ7RealTemp / count;
            obj.JCJ8RealTemp = JCJ8RealTemp / count;
            obj.JCJ9RealTemp = JCJ9RealTemp / count;
            obj.JCJ10RealTemp = JCJ10RealTemp / count;
            obj.ExtruderRevolvingRealSpeed = ExtruderRevolvingRealSpeed / count;
            obj.ExtruderRealCurrent = ExtruderRealCurrent / count;
            obj.RealRongZhi = RealRongZhi / count;
            obj.RealRemnant = RealRemnant / count;
            return obj;
        }

        public static JCJData ConvertDictionary2JCJData2(Dictionary<string, List<string>> dic, string proLineCode)
        {
            if (dic == null || dic.Count == 0)
                return null;
            JCJData obj = new JCJData();
            obj.ProductionLine = IDConvert.ProLineConvert(dic["ProLine1"][0], dic["ProLine2"][0]);
            obj.ProLineCode = proLineCode;
            obj.OrderID = IDConvert.OrderIDConvert(dic["OrderNum4"][0], dic["OrderNum3"][0], dic["OrderNum2"][0], dic["OrderNum1"][0]);
            obj.StartTime = DateTime.Parse(dic["Time"][0]);
            obj.EndTime = null;
            obj.UpdateTime = DateTime.Parse(dic["Time"][dic["Time"].Count - 1]);
            obj.Bal1Scale = float.Parse(dic["BalScale1"][0]) / 1000;
            obj.Bal2Scale = float.Parse(dic["BalScale2"][0]) / 1000;
            obj.Bal3Scale = float.Parse(dic["BalScale3"][0]) / 1000;
            obj.JCJ1TempOffset = float.Parse(dic["JTemOff1"][0]);
            obj.JCJ1SetTemp = float.Parse(dic["JTemSet1"][0]);
            obj.JCJ2TempOffset = float.Parse(dic["JTemOff2"][0]);
            obj.JCJ2SetTemp = float.Parse(dic["JTemSet2"][0]);
            obj.JCJ3TempOffset = float.Parse(dic["JTemOff3"][0]);
            obj.JCJ3SetTemp = float.Parse(dic["JTemSet3"][0]);
            obj.JCJ4TempOffset = float.Parse(dic["JTemOff4"][0]);
            obj.JCJ4SetTemp = float.Parse(dic["JTemSet4"][0]);
            obj.JCJ5TempOffset = float.Parse(dic["JTemOff5"][0]);
            obj.JCJ5SetTemp = float.Parse(dic["JTemSet5"][0]);
            obj.JCJ6TempOffset = float.Parse(dic["JTemOff6"][0]);
            obj.JCJ6SetTemp = float.Parse(dic["JTemSet6"][0]);
            obj.JCJ7TempOffset = float.Parse(dic["JTemOff7"][0]);
            obj.JCJ7SetTemp = float.Parse(dic["JTemSet7"][0]);
            obj.JCJ8TempOffset = float.Parse(dic["JTemOff8"][0]);
            obj.JCJ8SetTemp = float.Parse(dic["JTemSet8"][0]);
            obj.JCJ9TempOffset = float.Parse(dic["JTemOff9"][0]);
            obj.JCJ9SetTemp = float.Parse(dic["JTemSet9"][0]);
            obj.JCJ10TempOffset = float.Parse(dic["JTemOff10"][0]);
            obj.JCJ10SetTemp = float.Parse(dic["JTemSet10"][0]);
            obj.ExtruderMaxRevolvingSpeed = int.Parse(dic["JMaxRev"][0]);
            obj.ExtruderMinRevolvingSpeed = int.Parse(dic["JMinRev"][0]);
            obj.ExtruderMaxCurrent = float.Parse(dic["JMaxCur"][0]);
            obj.ExtruderMinCurrent = float.Parse(dic["JMinCur"][0]);
            obj.MaxRongZhi = float.Parse(dic["MaxRZ"][0]);
            obj.MinRongZhi = float.Parse(dic["MinRZ"][0]);
            obj.MaxRemnant = float.Parse(dic["MaxRemn"][0]);
            obj.MinRemnant = float.Parse(dic["MinRemn"][0]);
            if (dic.Keys.Contains("degreeH") && dic.Keys.Contains("degreeL"))
            {
                obj.StartPower = double.Parse(dic["degreeH"][0]) * 65535 + double.Parse(dic["degreeL"][0]);
                int count = dic["degreeH"].Count - 1;
                obj.EndPower = double.Parse(dic["degreeH"][count]) * 65535 + double.Parse(dic["degreeL"][count]);
            }
            float maxCurrent = float.MinValue;
            float minCurrent = float.MaxValue;
            float maxPower = float.MinValue;
            for (int i = 0; i < dic["Time"].Count; i++)
            {
                float Ia = float.Parse(dic["Ia"][i]);
                float Ib = float.Parse(dic["Ib"][i]);
                float Ic = float.Parse(dic["Ic"][i]);
                float ele = (Ia + Ib + Ic) / 3;
                maxCurrent = maxCurrent < ele ? ele : maxCurrent;
                if (ele > 0)
                    minCurrent = minCurrent > ele ? ele : minCurrent;
                float power = float.Parse(dic["power"][i]);
                maxPower = maxPower < power ? power : maxPower;
            }
            obj.MaxCurrent = maxCurrent;
            obj.MinCurrent = minCurrent;
            obj.MaxPower = maxPower;
            return obj;
        }
    }

    public class JCJDetail
    {
        public string ProductionLine { get; set; }
        public string ProLineCode { get; set; }
        public string OrderID { get; set; }
        public float Bal1RealCount { get; set; }
        public float Bal2RealCount { get; set; }
        public float Bal3RealCount { get; set; }
        public int WLJRevolvingSpeed { get; set; }
        public float JCJ1RealTemp { get; set; }
        public float JCJ2RealTemp { get; set; }
        public float JCJ3RealTemp { get; set; }
        public float JCJ4RealTemp { get; set; }
        public float JCJ5RealTemp { get; set; }
        public float JCJ6RealTemp { get; set; }
        public float JCJ7RealTemp { get; set; }
        public float JCJ8RealTemp { get; set; }
        public float JCJ9RealTemp { get; set; }
        public float JCJ10RealTemp { get; set; }
        public int ExtruderRevolvingRealSpeed { get; set; }
        public float ExtruderRealCurrent { get; set; }
        public float RealRongZhi { get; set; }
        public float RealRemnant { get; set; }
        public DateTime RecordTime { get; set; }
        public DateTime UpdateTime { get; set; }

        private readonly static int HalfHour = 1800;
        public static Dictionary<string, List<JCJDetail>> Convert2JCJDetail(Dictionary<string, List<string>> dic, string proLineCode)
        {
            if (dic == null || dic.Count == 0)
            {
                return null;
            }

            List<JCJDetail> list = new List<JCJDetail>();
            int count = dic["Time"].Count;
            for (int i = 0; i < count; i++)
            {
                JCJDetail obj = new JCJDetail();
                obj.ProductionLine = IDConvert.ProLineConvert(dic["ProLine1"][i], dic["ProLine2"][i]); ;
                obj.OrderID = IDConvert.OrderIDConvert(dic["OrderNum4"][i], dic["OrderNum3"][i], dic["OrderNum2"][i], dic["OrderNum1"][i]);
                obj.Bal1RealCount = float.Parse(dic["BalRCount1"][i]);
                obj.Bal2RealCount = float.Parse(dic["BalRCount2"][i]);
                obj.Bal3RealCount = float.Parse(dic["BalRCount3"][i]);
                obj.WLJRevolvingSpeed = int.Parse(dic["WLJRev"][i]);
                obj.JCJ1RealTemp = float.Parse(dic["JTemReal1"][i]);
                obj.JCJ2RealTemp = float.Parse(dic["JTemReal2"][i]);
                obj.JCJ3RealTemp = float.Parse(dic["JTemReal3"][i]);
                obj.JCJ4RealTemp = float.Parse(dic["JTemReal4"][i]);
                obj.JCJ5RealTemp = float.Parse(dic["JTemReal5"][i]);
                obj.JCJ6RealTemp = float.Parse(dic["JTemReal6"][i]);
                obj.JCJ7RealTemp = float.Parse(dic["JTemReal7"][i]);
                obj.JCJ8RealTemp = float.Parse(dic["JTemReal8"][i]);
                obj.JCJ9RealTemp = float.Parse(dic["JTemReal9"][i]);
                obj.JCJ10RealTemp = float.Parse(dic["JTemReal10"][i]);
                obj.ExtruderRevolvingRealSpeed = int.Parse(dic["JRealRev"][i]);
                obj.ExtruderRealCurrent = float.Parse(dic["JRealCur"][i]) / 10;
                obj.RealRongZhi = float.Parse(dic["RealRz"][i]);
                obj.RealRemnant = float.Parse(dic["RealRemn"][i]);
                obj.RecordTime = DateTime.Parse(dic["Time"][i]);
                obj.UpdateTime = DateTime.Parse(dic["Time"][i]);
                obj.ProLineCode = proLineCode;
                list.Add(obj);
            }

            List<JCJDetail> finalList = new List<JCJDetail>();
            // 以1800秒（30分钟）为间隔来整合挤出机数据
            DateTime nextTime = list[0].RecordTime.AddSeconds(HalfHour);
            List<JCJDetail> tmpList = new List<JCJDetail>();
            foreach (JCJDetail item in list)
            {
                tmpList.Add(item);
                // 以30分钟为间隔来整合挤出机数据
                if (DateTime.Compare(item.RecordTime, nextTime) >= 0)
                {
                    finalList.Add(JCJDetail.MergerJCJDataList(tmpList));
                    tmpList.Clear();
                    nextTime = item.RecordTime.AddSeconds(HalfHour);
                }
            }
            if (tmpList.Count > 0)
                finalList.Add(JCJDetail.MergerJCJDataList(tmpList));

            Dictionary<string, List<JCJDetail>> retDic = new Dictionary<string, List<JCJDetail>>();
            if (finalList.Count > 0)
                retDic.Add(finalList[0].OrderID, finalList);
            return retDic;
        }
        private static JCJDetail MergerJCJDataList(List<JCJDetail> list)
        {
            if (list == null || list.Count == 0)
                return null;
            else if (list.Count == 1)
                return list[0];

            int count = list.Count;
            JCJDetail obj = new JCJDetail();
            obj.ProductionLine = list[0].ProductionLine;
            obj.ProLineCode = list[0].ProLineCode;
            obj.OrderID = list[0].OrderID;
            obj.RecordTime = list[0].RecordTime;
            obj.UpdateTime = list[count - 1].UpdateTime;
            float Bal1RealCount = 0.0f, Bal2RealCount = 0.0f, Bal3RealCount = 0.0f;
            float JCJ1RealTemp = 0.0f, JCJ2RealTemp = 0.0f, JCJ3RealTemp = 0.0f, JCJ4RealTemp = 0.0f, JCJ5RealTemp = 0.0f, JCJ6RealTemp = 0.0f, JCJ7RealTemp = 0.0f, JCJ8RealTemp = 0.0f, JCJ9RealTemp = 0.0f, JCJ10RealTemp = 0.0f;
            int WLJRevolvingSpeed = 0, ExtruderRevolvingRealSpeed = 0;
            float ExtruderRealCurrent = 0.0f, RealRongZhi = 0.0f, RealRemnant = 0.0f;
            for (int i = 0; i < count; i++)
            {
                Bal1RealCount += list[i].Bal1RealCount;
                Bal2RealCount += list[i].Bal2RealCount;
                Bal3RealCount += list[i].Bal3RealCount;
                WLJRevolvingSpeed += list[i].WLJRevolvingSpeed;
                JCJ1RealTemp += list[i].JCJ1RealTemp;
                JCJ2RealTemp += list[i].JCJ2RealTemp;
                JCJ3RealTemp += list[i].JCJ3RealTemp;
                JCJ4RealTemp += list[i].JCJ4RealTemp;
                JCJ5RealTemp += list[i].JCJ5RealTemp;
                JCJ6RealTemp += list[i].JCJ6RealTemp;
                JCJ7RealTemp += list[i].JCJ7RealTemp;
                JCJ8RealTemp += list[i].JCJ8RealTemp;
                JCJ9RealTemp += list[i].JCJ9RealTemp;
                JCJ10RealTemp += list[i].JCJ10RealTemp;
                Bal3RealCount += list[i].Bal3RealCount;
                Bal1RealCount += list[i].Bal1RealCount;
                Bal2RealCount += list[i].Bal2RealCount;
                Bal3RealCount += list[i].Bal3RealCount;
                ExtruderRevolvingRealSpeed += list[i].ExtruderRevolvingRealSpeed;
                ExtruderRealCurrent += list[i].ExtruderRealCurrent;
                RealRongZhi += list[i].RealRongZhi;
                RealRemnant += list[i].RealRemnant;
            }
            obj.Bal1RealCount = Bal1RealCount / count;
            obj.Bal2RealCount = Bal2RealCount / count;
            obj.Bal3RealCount = Bal3RealCount / count;
            obj.WLJRevolvingSpeed = WLJRevolvingSpeed / count;
            obj.JCJ1RealTemp = JCJ1RealTemp / count;
            obj.JCJ2RealTemp = JCJ2RealTemp / count;
            obj.JCJ3RealTemp = JCJ3RealTemp / count;
            obj.JCJ4RealTemp = JCJ4RealTemp / count;
            obj.JCJ5RealTemp = JCJ5RealTemp / count;
            obj.JCJ6RealTemp = JCJ6RealTemp / count;
            obj.JCJ7RealTemp = JCJ7RealTemp / count;
            obj.JCJ8RealTemp = JCJ8RealTemp / count;
            obj.JCJ9RealTemp = JCJ9RealTemp / count;
            obj.JCJ10RealTemp = JCJ10RealTemp / count;
            obj.ExtruderRevolvingRealSpeed = ExtruderRevolvingRealSpeed / count;
            obj.ExtruderRealCurrent = ExtruderRealCurrent / count;
            obj.RealRongZhi = RealRongZhi / count;
            obj.RealRemnant = RealRemnant / count;
            return obj;
        }
    }

    public class JCJDataInfo
    {
        public List<JCJSummaryInfo> JCJSummaryInfo { get; set; }
        public List<JCJDetailInfo> JCJDetailInfo { get; set; }
    }

    public class JCJSummaryInfo
    {
        public string ProductionLine { get; set; }
        public string ProLineCode { get; set; }
        public string OrderID { get; set; }
        public DateTime? StartTime { get; set; }
        public DateTime? EndTime { get; set; }
        public DateTime UpdateTime { get; set; }

        public static JCJSummaryInfo ConvertDictionary2SummaryWithEndTime(Dictionary<string, string> dic, string proLineCode)
        {
            if (dic == null || dic.Count == 0)
                return null;

            JCJSummaryInfo obj = new JCJSummaryInfo();
            obj.ProductionLine = IDConvert.ProLineConvert(dic["ProLine1"], dic["ProLine2"]);
            obj.ProLineCode = proLineCode;
            obj.OrderID = IDConvert.OrderIDConvert(dic["OrderNum4"], dic["OrderNum3"], dic["OrderNum2"], dic["OrderNum1"]);
            if (!string.IsNullOrEmpty(dic["Time"]))
            {
                obj.EndTime = DateTime.Parse(dic["Time"]);
                obj.UpdateTime = DateTime.Parse(dic["Time"]);
            }
            return obj;
        }

        public static JCJSummaryInfo ConvertDictionary2Summary(Dictionary<string, List<string>> dic, string proLineCode)
        {
            if (dic == null || dic.Count == 0)
                return null;
            JCJSummaryInfo obj = new JCJSummaryInfo();
            obj.ProductionLine = IDConvert.ProLineConvert(dic["ProLine1"][0], dic["ProLine2"][0]);
            obj.ProLineCode = proLineCode;
            obj.OrderID = IDConvert.OrderIDConvert(dic["OrderNum4"][0], dic["OrderNum3"][0], dic["OrderNum2"][0], dic["OrderNum1"][0]);
            obj.StartTime = DateTime.Parse(dic["Time"][0]);
            obj.EndTime = null;
            obj.UpdateTime = DateTime.Parse(dic["Time"][dic["Time"].Count - 1]);
            return obj;
        }
    }

    public class JCJDetailInfo
    {
        public string ProductionLine { get; set; }
        public string ProLineCode { get; set; }
        public float Bal1Scale { get; set; }
        public float Bal2Scale { get; set; }
        public float Bal3Scale { get; set; }
        public float Bal1RealCount { get; set; }
        public float Bal2RealCount { get; set; }
        public float Bal3RealCount { get; set; }
        public int WLJRevolvingSpeed { get; set; }
        public float JCJ1TempOffset { get; set; }
        public float JCJ2TempOffset { get; set; }
        public float JCJ3TempOffset { get; set; }
        public float JCJ4TempOffset { get; set; }
        public float JCJ5TempOffset { get; set; }
        public float JCJ6TempOffset { get; set; }
        public float JCJ7TempOffset { get; set; }
        public float JCJ8TempOffset { get; set; }
        public float JCJ9TempOffset { get; set; }
        public float JCJ10TempOffset { get; set; }
        public float JCJ1SetTemp { get; set; }
        public float JCJ2SetTemp { get; set; }
        public float JCJ3SetTemp { get; set; }
        public float JCJ4SetTemp { get; set; }
        public float JCJ5SetTemp { get; set; }
        public float JCJ6SetTemp { get; set; }
        public float JCJ7SetTemp { get; set; }
        public float JCJ8SetTemp { get; set; }
        public float JCJ9SetTemp { get; set; }
        public float JCJ10SetTemp { get; set; }
        public float JCJ1RealTemp { get; set; }
        public float JCJ2RealTemp { get; set; }
        public float JCJ3RealTemp { get; set; }
        public float JCJ4RealTemp { get; set; }
        public float JCJ5RealTemp { get; set; }
        public float JCJ6RealTemp { get; set; }
        public float JCJ7RealTemp { get; set; }
        public float JCJ8RealTemp { get; set; }
        public float JCJ9RealTemp { get; set; }
        public float JCJ10RealTemp { get; set; }
        public int ExtruderMaxRevolvingSpeed { get; set; }
        public int ExtruderMinRevolvingSpeed { get; set; }
        public int ExtruderRevolvingRealSpeed { get; set; }
        public float ExtruderMaxCurrent { get; set; }
        public float ExtruderMinCurrent { get; set; }
        public float ExtruderRealCurrent { get; set; }
        public float MaxRongZhi { get; set; }
        public float MinRongZhi { get; set; }
        public float RealRongZhi { get; set; }
        public float MaxRemnant { get; set; }
        public float MinRemnant { get; set; }
        public float RealRemnant { get; set; }
        public float MaxCurrent { get; set; }
        public float MinCurrent { get; set; }
        public float Current { get; set; }
        public float MaxPower { get; set; }
        public double ElectricPower { get; set; }
        public DateTime RecordTime { get; set; }
        public DateTime UpdateTime { get; set; }
        public int JsProduce { get; set; }

        private readonly static int Interval = 180;

        public static List<JCJDetailInfo> ConvertDictionary2JCJDetails(Dictionary<string, List<string>> dic, string proLineCode)
        {
            if (dic == null || dic.Count == 0)
                return null;
            List<JCJDetailInfo> retList = new List<JCJDetailInfo>();
            DateTime startTime = DateTime.Now;
            if (dic["Time"] != null && dic["Time"].Count > 0)
                startTime = DateTime.Parse(dic["Time"][0]).AddSeconds(Interval);
            int count = dic["Time"].Count;
            Dictionary<string, List<string>> newDic = new Dictionary<string, List<string>>();
            for(int i = 0; i < count; i++)
            {
                DateTime currTime = DateTime.Parse(dic["Time"][i]);
                // 以3分钟为间隔记录数据
                if (currTime >= startTime)
                {
                    if (newDic.Count > 0)
                    {
                        JCJDetailInfo data = JCJDetailInfo.ConvertDictionary2JCJDetailInfo(newDic, proLineCode);
                        if (data != null)
                            retList.Add(data);
                    }
                    newDic.Clear();
                    startTime = currTime.AddSeconds(Interval);
                }
                foreach (KeyValuePair<string, List<string>> item in dic)
                {
                    if (newDic.ContainsKey(item.Key))
                        newDic[item.Key].Add(item.Value[i]);
                    else
                        newDic.Add(item.Key, new List<string> { item.Value[i] });
                }
            }
            // 将剩余的数据归纳成一个记录
            if (newDic.Count > 0)
            {
                JCJDetailInfo data = JCJDetailInfo.ConvertDictionary2JCJDetailInfo(newDic, proLineCode);
                if (data != null)
                    retList.Add(data);
            }
            return retList;
        }

        private static JCJDetailInfo ConvertDictionary2JCJDetailInfo(Dictionary<string, List<string>> dic, string proLineCode)
        {
            if (dic == null || dic.Count == 0)
                return null;
            int count = dic["Time"].Count - 1;
            JCJDetailInfo obj = new JCJDetailInfo();
            obj.ProductionLine = IDConvert.ProLineConvert(dic["ProLine1"][0], dic["ProLine2"][0]);
            obj.ProLineCode = proLineCode;
            obj.RecordTime = DateTime.Parse(dic["Time"][count]);
            obj.UpdateTime = DateTime.Parse(dic["Time"][count]);
            obj.Bal1Scale = float.Parse(dic["BalScale1"][count]) / 1000;
            obj.Bal2Scale = float.Parse(dic["BalScale2"][count]) / 1000;
            obj.Bal3Scale = float.Parse(dic["BalScale3"][count]) / 1000;
            obj.Bal1RealCount = float.Parse(dic["BalRCount1"][count]);
            obj.Bal2RealCount = float.Parse(dic["BalRCount2"][count]);
            obj.Bal3RealCount = float.Parse(dic["BalRCount3"][count]);
            obj.WLJRevolvingSpeed = int.Parse(dic["WLJRev"][count]);
            obj.JCJ1TempOffset = float.Parse(dic["JTemOff1"][count]);
            obj.JCJ2TempOffset = float.Parse(dic["JTemOff2"][count]);
            obj.JCJ3TempOffset = float.Parse(dic["JTemOff3"][count]);
            obj.JCJ4TempOffset = float.Parse(dic["JTemOff4"][count]);
            obj.JCJ5TempOffset = float.Parse(dic["JTemOff5"][count]);
            obj.JCJ6TempOffset = float.Parse(dic["JTemOff6"][count]);
            obj.JCJ7TempOffset = float.Parse(dic["JTemOff7"][count]);
            obj.JCJ8TempOffset = float.Parse(dic["JTemOff8"][count]);
            obj.JCJ9TempOffset = float.Parse(dic["JTemOff9"][count]);
            obj.JCJ10TempOffset = float.Parse(dic["JTemOff10"][count]);
            obj.JCJ1SetTemp = float.Parse(dic["JTemSet1"][count]);
            obj.JCJ2SetTemp = float.Parse(dic["JTemSet2"][count]);
            obj.JCJ3SetTemp = float.Parse(dic["JTemSet3"][count]);
            obj.JCJ4SetTemp = float.Parse(dic["JTemSet4"][count]);
            obj.JCJ5SetTemp = float.Parse(dic["JTemSet5"][count]);
            obj.JCJ6SetTemp = float.Parse(dic["JTemSet6"][count]);
            obj.JCJ7SetTemp = float.Parse(dic["JTemSet7"][count]);
            obj.JCJ8SetTemp = float.Parse(dic["JTemSet8"][count]);
            obj.JCJ9SetTemp = float.Parse(dic["JTemSet9"][count]);
            obj.JCJ10SetTemp = float.Parse(dic["JTemSet10"][count]);
            obj.JCJ1RealTemp = float.Parse(dic["JTemReal1"][count]);
            obj.JCJ2RealTemp = float.Parse(dic["JTemReal2"][count]);
            obj.JCJ3RealTemp = float.Parse(dic["JTemReal3"][count]);
            obj.JCJ4RealTemp = float.Parse(dic["JTemReal4"][count]);
            obj.JCJ5RealTemp = float.Parse(dic["JTemReal5"][count]);
            obj.JCJ6RealTemp = float.Parse(dic["JTemReal6"][count]);
            obj.JCJ7RealTemp = float.Parse(dic["JTemReal7"][count]);
            obj.JCJ8RealTemp = float.Parse(dic["JTemReal8"][count]);
            obj.JCJ9RealTemp = float.Parse(dic["JTemReal9"][count]);
            obj.JCJ10RealTemp = float.Parse(dic["JTemReal10"][count]);
            obj.ExtruderMaxRevolvingSpeed = int.Parse(dic["JMaxRev"][count]);
            obj.ExtruderMinRevolvingSpeed = int.Parse(dic["JMinRev"][count]);
            obj.ExtruderRevolvingRealSpeed = int.Parse(dic["JRealRev"][count]);
            obj.ExtruderMaxCurrent = float.Parse(dic["JMaxCur"][count]);
            obj.ExtruderMinCurrent = float.Parse(dic["JMinCur"][count]);
            obj.ExtruderRealCurrent = float.Parse(dic["JRealCur"][count]) / 10;
            obj.MaxRongZhi = float.Parse(dic["MaxRZ"][count]);
            obj.MinRongZhi = float.Parse(dic["MinRZ"][count]);
            obj.RealRongZhi = float.Parse(dic["RealRz"][count]);
            obj.MaxRemnant = float.Parse(dic["MaxRemn"][count]);
            obj.MinRemnant = float.Parse(dic["MinRemn"][count]);
            obj.RealRemnant = float.Parse(dic["RealRemn"][count]);
            float maxCurrent = float.MinValue;
            float minCurrent = float.MaxValue;
            float maxPower = float.MinValue;
            for(int i = 0; i <= count; i++)
            {
                float Ia = float.Parse(dic["Ia"][i]);
                float Ib = float.Parse(dic["Ib"][i]);
                float Ic = float.Parse(dic["Ic"][i]);
                float ele = (Ia + Ib + Ic) / 3;
                maxCurrent = maxCurrent < ele ? ele : maxCurrent;
                minCurrent = minCurrent > ele ? ele : minCurrent;
                float power = float.Parse(dic["power"][i]);
                maxPower = maxPower < power ? power : maxPower;
                obj.Current = ele;
            }
            obj.MaxCurrent = maxCurrent;
            obj.MinCurrent = minCurrent;
            obj.MaxPower = maxPower;
            obj.ElectricPower = double.Parse(dic["degreeH"][count]) * 65535 + double.Parse(dic["degreeL"][count]);
            return obj;
        }
    }

    public class JHJDataInfo
    {
        public List<JHJData> JHJInfoList { get; set; }
        public List<JHJDetailInfo> JHJDetailList { get; set; }
    }

    /// <summary>
    /// 均化机数据——对应DB中的JHJInfo
    /// </summary>
    public class JHJData
    {
        public string ProductionLine { get; set; }
        public string ProLineCode { get; set; }
        public string OrderID { get; set; }
        public DateTime? StartTime { get; set; }
        public DateTime? EndTime { get; set; }
        public DateTime UpdateTime { get; set; }
        public int Number { get; set; }
        public float UnitWeight { get; set; }
        public float StartPower { get; set; }
        public float EndPower { get; set; }
        // A桶至B桶的吸料时间记录列表
        public List<ABRecordTime> ABTimeList { get; set; }
        // B桶加热时间记录列表
        public List<BHeatingRecordTime> BHeatingTimeList { get; set; }
        // B桶放料时间记录列表
        public List<BPackingRecordTime> BPackingTimeList { get; set; }
        public List<BThrowRecordTime> BThrowTimeList { get; set; }
        public float MaxCurrent { get; set; }
        public float MinCurrent { get; set; }
        public float MaxPower { get; set; }


        public static JHJData ConvertDictionary2JHJData(Dictionary<string, string> varDic, string prolineCode)
        {
            if (varDic == null || varDic.Count == 0)
                return null;

            JHJData retObj = new JHJData();
            //retObj.ProductionLine = IDConvert.ProLineConvert(varDic["ProLine1"], varDic["ProLine2"]);
            retObj.OrderID = IDConvert.OrderIDConvert(varDic["OrderNum4"], varDic["OrderNum3"], varDic["OrderNum2"], varDic["OrderNum1"]);
            retObj.StartTime = null;
            retObj.EndTime = DateTime.Parse(varDic["Time"]);
            retObj.UpdateTime = DateTime.Parse(varDic["Time"]);
            retObj.Number = int.Parse(varDic["UnitTC"]);
            retObj.ProductionLine = "A" + prolineCode.Substring(2);
            retObj.ProLineCode = prolineCode;
            if (varDic.Keys.Contains("degree"))
                retObj.EndPower = float.Parse(varDic["degree"]);
            //retObj.Weight = float.Parse(varDic["Count"]);
            retObj.MaxCurrent = float.MinValue;
            retObj.MinCurrent = float.MaxValue;
            retObj.MaxPower = float.MinValue;
            return retObj;
        }

        public static JHJData ConvertDictionary2JHJData(Dictionary<string, List<string>> varDic, string prolineCode)
        {
            if (varDic == null || varDic.Count == 0)
                return null;

            JHJData retObj = new JHJData();
            //retObj.ProductionLine = IDConvert.ProLineConvert(varDic["ProLine1"][0], varDic["ProLine2"][0]);
            retObj.ProductionLine = "A" + prolineCode.Substring(2);
            retObj.ProLineCode = prolineCode;
            retObj.OrderID = IDConvert.OrderIDConvert(varDic["OrderNum4"][0], varDic["OrderNum3"][0], varDic["OrderNum2"][0], varDic["OrderNum1"][0]);
            retObj.StartTime = DateTime.Parse(varDic["Time"][0]);
            retObj.UnitWeight = float.Parse(varDic["UnitPC"][0]);
            retObj.EndTime = null;
            retObj.UpdateTime = DateTime.Parse(varDic["Time"][varDic["Time"].Count - 1]);
            retObj.MaxCurrent = float.MinValue;
            retObj.MinCurrent = float.MaxValue;
            retObj.MaxPower = float.MinValue;
            if (varDic.Keys.Contains("degree") && varDic["degree"].Count > 0)
            {
                retObj.StartPower = float.Parse(varDic["degree"][0]);
                int index = varDic["degree"].Count - 1;
                retObj.EndPower = float.Parse(varDic["degree"][index]);
            }
            //retObj.Weight = float.Parse(varDic["Count"][0]);
            List<DateTime> timeList = new List<DateTime>();
            List<string> signABList = new List<string>();
            List<string> signBHeatingList = new List<string>();
            List<string> signBPackingList = new List<string>();
            List<float> signBTempList = new List<float>();
            List<float> signARealTemList = new List<float>();
            //List<string> signBThrowList = new List<string>();
            //List<float> signBWeightList = new List<float>();
            List<int> packNumList = new List<int>();
            int count = varDic["Time"].Count;
            for (int i = 0; i < count; i++)
            {
                DateTime time = DateTime.Parse(varDic["Time"][i]);
                timeList.Add(time);
                signABList.Add(varDic["ABStartT"][i]);
                signBHeatingList.Add(varDic["BHeatST"][i]);
                signBPackingList.Add(varDic["BThrowST"][i]);
                signBTempList.Add(float.Parse(varDic["ZDTemper"][i]) / 10);
                signARealTemList.Add(float.Parse(varDic["ARealTem"][i]) / 10);
                packNumList.Add(int.Parse(varDic["UnitTC"][i]));

                float Ia = float.Parse(varDic["Ia"][i]);
                float Ib = float.Parse(varDic["Ib"][i]);
                float Ic = float.Parse(varDic["Ic"][i]);
                float ele = (Ia + Ib + Ic) / 3;
                retObj.MaxCurrent = retObj.MaxCurrent < ele ? ele : retObj.MaxCurrent;
                if (ele > 0)
                    retObj.MinCurrent = retObj.MinCurrent > ele ? ele : retObj.MinCurrent;
                float maxPower = float.Parse(varDic["power"][i]);
                retObj.MaxPower = retObj.MaxPower < maxPower ? maxPower : retObj.MaxPower;
            }
            retObj.ABTimeList = GetABRecordTimeList(timeList, signABList, signARealTemList);
            retObj.BHeatingTimeList = GetBHeatingRecordTimeList(timeList, signBHeatingList);
            retObj.BPackingTimeList = GetBPackingRecordTimeList(timeList, signBPackingList, signBTempList, packNumList);
            //retObj.BThrowTimeList = GetBThrowRecordTimeList(timeList, signBThrowList, signBWeightList);
            retObj.Number = int.Parse(varDic["UnitTC"][count - 1]);
            return retObj;
        }

        /// <summary>
        /// 更具记录时间和标记位，计算出这个时间段内的A桶到B桶的吸料事件列表
        /// </summary>
        /// <param name="timeList"></param>
        /// <param name="signList"></param>
        /// <returns></returns>
        private static List<ABRecordTime> GetABRecordTimeList(List<DateTime> timeList, List<string> signList, List<float> tempList)
        {
            if (timeList.Count == 0 || timeList.Count != signList.Count)
                return null;

            List<ABRecordTime> retList = new List<ABRecordTime>();
            ABRecordTime recordTime = new ABRecordTime();
            recordTime.StartTime = null;
            recordTime.EndTime = null;
            // 如果当前列表的第一个值为1，记录开始时间
            if(signList[0] == "1")
            {
                recordTime.StartTime = timeList[0];
                recordTime.RealTemperature = tempList[0];
            }
            // 如果当前列表的第一个值为0，只记录结束时间
            else
            {
                recordTime.EndTime = timeList[0];
                retList.Add(recordTime);
                recordTime = null;
            }

            for (int i = 1; i < timeList.Count; i++ )
            {
                if (signList[i] == "1")
                {
                    // 当前为一个新的时间
                    if (signList[i - 1] == "0")
                    {
                        recordTime = new ABRecordTime();
                        recordTime.StartTime = timeList[i];
                        recordTime.EndTime = null;
                        recordTime.RealTemperature = tempList[i];
                    }
                }
                else
                {
                    // 如果前一个为1，则表明当前的时间是结束时间
                    if (signList[i-1] == "1")
                    {
                        if (recordTime != null)
                        {
                            recordTime.EndTime = timeList[i - 1];
                            retList.Add(recordTime);
                            recordTime = null;
                        }
                    }
                }
            }

            if (recordTime != null)
                retList.Add(recordTime);
            return retList;
        }

        private static List<BHeatingRecordTime> GetBHeatingRecordTimeList(List<DateTime> timeList, List<string> signList)
        {
            if (timeList.Count == 0 || timeList.Count != signList.Count)
                return null;

            List<BHeatingRecordTime> retList = new List<BHeatingRecordTime>();
            BHeatingRecordTime recordTime = new BHeatingRecordTime();
            recordTime.StartTime = null;
            recordTime.EndTime = null;
            // 如果当前列表的第一个值为1，记录开始时间
            if (signList[0] == "1")
            {
                recordTime.StartTime = timeList[0];
            }
            // 如果当前列表的第一个值为0，只记录结束时间
            else
            {
                recordTime.EndTime = timeList[0];
                retList.Add(recordTime);
                recordTime = null;
            }

            for (int i = 1; i < timeList.Count; i++)
            {
                if (signList[i] == "1")
                {
                    // 当前为一个新的时间
                    if (signList[i - 1] == "0")
                    {
                        recordTime = new BHeatingRecordTime();
                        recordTime.StartTime = timeList[i];
                        recordTime.EndTime = null;
                    }
                }
                else
                {
                    // 如果前一个为1，则表明当前的时间是结束时间
                    if (signList[i - 1] == "1")
                    {
                        if (recordTime != null)
                        {
                            recordTime.EndTime = timeList[i - 1];
                            retList.Add(recordTime);
                            recordTime = null;
                        }
                    }
                }
            }

            if (recordTime != null)
                retList.Add(recordTime);
            return retList;
        }

        private static List<BPackingRecordTime> GetBPackingRecordTimeList(List<DateTime> timeList, List<string> signList, List<float> tempList, List<int> packNumList)
        {
            if (timeList.Count == 0 || timeList.Count != signList.Count)
                return null;

            List<BPackingRecordTime> retList = new List<BPackingRecordTime>();
            BPackingRecordTime recordTime = new BPackingRecordTime();
            recordTime.StartTime = null;
            recordTime.EndTime = null;
            // 如果当前列表的第一个值为1，记录开始时间
            if (signList[0] == "1")
            {
                recordTime.StartTime = timeList[0];
                recordTime.StartNum = packNumList[0];
                recordTime.EndNum = packNumList[0];
                recordTime.Temperature = tempList[0];
            }
            // 如果当前列表的第一个值为0，只记录结束时间
            else
            {
                recordTime.EndTime = timeList[0];
                retList.Add(recordTime);
                recordTime = null;
            }

            for (int i = 1; i < timeList.Count; i++)
            {
                if (signList[i] == "1")
                {
                    // 当前为一个新的时间
                    if (signList[i - 1] == "0")
                    {
                        recordTime = new BPackingRecordTime();
                        recordTime.StartTime = timeList[i];
                        recordTime.StartNum = packNumList[i];
                        recordTime.EndTime = null;
                        recordTime.Temperature = tempList[i];
                    }
                    else
                        recordTime.EndNum = packNumList[i];
                }
                else
                {
                    // 如果前一个为1，则表明当前的时间是结束时间
                    if (signList[i - 1] == "1")
                    {
                        if (recordTime != null)
                        {
                            recordTime.EndTime = timeList[i - 1];
                            recordTime.EndNum = packNumList[i - 1];
                            retList.Add(recordTime);
                            recordTime = null;
                        }
                    }
                }
            }

            if (recordTime != null)
                retList.Add(recordTime);
            return retList;
        }
        private static List<BThrowRecordTime> GetBThrowRecordTimeList(List<DateTime> timeList, List<string> signList, List<float> weightList)
        {
            if (timeList.Count == 0 || timeList.Count != signList.Count)
                return null;

            List<BThrowRecordTime> retList = new List<BThrowRecordTime>();
            BThrowRecordTime recordTime = new BThrowRecordTime();
            recordTime.StartTime = null;
            recordTime.EndTime = null;
            // 如果当前列表的第一个值为1，记录开始时间
            if (signList[0] == "1")
            {
                recordTime.StartTime = timeList[0];
            }
            // 如果当前列表的第一个值为0，只记录结束时间
            else
            {
                recordTime.EndTime = timeList[0];
                retList.Add(recordTime);
                recordTime = null;
            }

            for (int i = 1; i < timeList.Count; i++)
            {
                if (signList[i] == "1")
                {
                    // 当前为一个新的时间
                    if (signList[i - 1] == "0")
                    {
                        recordTime = new BThrowRecordTime();
                        recordTime.StartTime = timeList[i];
                        recordTime.EndTime = null;
                    }
                }
                else
                {
                    // 如果前一个为1，则表明当前的时间是结束时间
                    if (signList[i - 1] == "1")
                    {
                        if (recordTime != null)
                        {
                            recordTime.EndTime = timeList[i - 1];
                            recordTime.Weight = weightList[i - 1];
                            retList.Add(recordTime);
                            recordTime = null;
                        }
                    }
                }
            }

            if (recordTime != null)
                retList.Add(recordTime);
            return retList;
        }
    }

    public class ABRecordTime
    {
        public string ProductionLine { get; set; }
        public string OrderID { get; set; }
        public string ProLineCode { get; set; }
        // A桶至B桶的吸料开始时间
        public DateTime? StartTime { get; set; }
        // A桶至B桶吸料结束时间
        public DateTime? EndTime { get; set; }
        public double RealTemperature { get; set; }
    }
    public class BHeatingRecordTime
    {
        public string ProductionLine { get; set; }
        public string OrderID { get; set; }
        public string ProLineCode { get; set; }
        // B桶加热开始时间
        public DateTime? StartTime { get; set; }
        // B桶加热结束时间
        public DateTime? EndTime { get; set; }
    }
    public class BPackingRecordTime
    {
        public string ProductionLine { get; set; }
        public string OrderID { get; set; }
        public string ProLineCode { get; set; }
        // 放料时锥温度
        public double Temperature { get; set; }
        // B桶放料开始时间
        public DateTime? StartTime { get; set; }
        // B桶放料结束时间
        public DateTime? EndTime { get; set; }
        // 整体重量
        public float Weight { get; set; }
        // 放料次数
        public int OpenNum { get; set; }
        // 开始时包装数量
        public int StartNum { get; set; }
        // 结束时包装数量
        public int EndNum { get; set; }
    }
    public class BThrowRecordTime
    {
        // 单包的放料开始时间
        public DateTime? StartTime { get; set; }
        // 单包的放料结束时间
        public DateTime? EndTime { get; set; }
        // 单包重量
        public float Weight { get; set; }
    }
    public class JHJDetailInfo
    {
        public string ProductionLine { get; set; }
        public string ProLineCode { get; set; }
        public DateTime RecordTime { get; set; }
        public float MaxCurrent { get; set; }
        public float MinCurrent { get; set; }
        public float MaxPower { get; set; }
        public double ElectricPower { get; set; }
        public float BBoxZDTemper { get; set; }
        public float ABoxTemper { get; set; }
        public int PackNumber { get; set; }
        // 3分钟为间隔记录数据到DB中
        private readonly static int Interval = 180;

        public static List<JHJDetailInfo> ConvertDictionary2JHJDetails(Dictionary<string, List<string>> dic, string proLineCode)
        {
            if (dic == null || dic.Count == 0)
                return null;
            List<JHJDetailInfo> retList = new List<JHJDetailInfo>();
            DateTime startTime = DateTime.Now;
            if (dic["Time"] != null && dic["Time"].Count > 0)
                startTime = DateTime.Parse(dic["Time"][0]).AddSeconds(Interval);
            int count = dic["Time"].Count;
            Dictionary<string, List<string>> newDic = new Dictionary<string, List<string>>();
            for (int i = 0; i < count; i++)
            {
                DateTime currTime = DateTime.Parse(dic["Time"][i]);
                // 以3分钟为间隔记录数据
                if (currTime >= startTime)
                {
                    if (newDic.Count > 0)
                    {
                        JHJDetailInfo data = JHJDetailInfo.ConvertDictionary2JHJDetailInfo(newDic, proLineCode);
                        if (data != null)
                            retList.Add(data);
                    }
                    newDic.Clear();
                    startTime = currTime.AddSeconds(Interval);
                }
                foreach (KeyValuePair<string, List<string>> item in dic)
                {
                    if (newDic.ContainsKey(item.Key))
                        newDic[item.Key].Add(item.Value[i]);
                    else
                        newDic.Add(item.Key, new List<string> { item.Value[i] });
                }
            }
            // 将剩余的数据归纳成一个记录
            if (newDic.Count > 0)
            {
                JHJDetailInfo data = JHJDetailInfo.ConvertDictionary2JHJDetailInfo(newDic, proLineCode);
                if (data != null)
                    retList.Add(data);
            }
            return retList;
        }
        private static JHJDetailInfo ConvertDictionary2JHJDetailInfo(Dictionary<string, List<string>> dic, string proLineCode)
        {
            if (dic == null || dic.Count == 0)
                return null;
            int count = dic["Time"].Count - 1;
            JHJDetailInfo retObj = new JHJDetailInfo();
            //retObj.ProductionLine = IDConvert.ProLineConvert(dic["ProLine1"][0], dic["ProLine2"][0]);
            retObj.ProductionLine = "A" + proLineCode.Substring(2);
            retObj.ProLineCode = proLineCode;
            retObj.RecordTime = DateTime.Parse(dic["Time"][count]);
            retObj.ElectricPower = float.Parse(dic["degree"][count]);
            retObj.BBoxZDTemper = float.Parse(dic["ZDTemper"][count]);
            retObj.ABoxTemper = float.Parse(dic["ARealTem"][count]);
            retObj.PackNumber = int.Parse(dic["UnitTC"][count]);
            float maxCurrent = float.MinValue;
            float minCurrent = float.MaxValue;
            float maxPower = float.MinValue;
            for (int i = 0; i <= count; i++)
            {
                float Ia = float.Parse(dic["Ia"][i]);
                float Ib = float.Parse(dic["Ib"][i]);
                float Ic = float.Parse(dic["Ic"][i]);
                float ele = (Ia + Ib + Ic) / 3;
                maxCurrent = maxCurrent < ele ? ele : maxCurrent;
                if (ele > 0)
                    minCurrent = minCurrent > ele ? ele : minCurrent;
                float power = float.Parse(dic["power"][i]);
                maxPower = maxPower < power ? power : maxPower;
            }
            retObj.MaxCurrent = maxCurrent;
            retObj.MinCurrent = minCurrent;
            retObj.MaxPower = maxPower;
            return retObj;
        }
    }

    public class MeterData
    {
        public string MeterID { get; set; }
        public string MeterPlace { get; set; }
        public DateTime RecordTime { get; set; }
        public float PeakUnitPrice { get; set; }
        public float GenUnitPrice { get; set; }
        public float LowUnitPrice { get; set; }
        public float PeakStartQuantity { get; set; }
        public float PeakEndQuantity { get; set; }
        public float GenStartQuantity { get; set; }
        public float GenEndQuantity { get; set; }
        public float LowStartQuantity { get; set; }
        public float LowEndQuantity { get; set; }
        public float MaxCurrent { get; set; }
        public float MinCurrent { get; set; }
        public float MaxLoad { get; set; }

        public static MeterData ConvertDictionary2MeterData(Dictionary<string, List<string>> varDic)
        {
            if (varDic == null || varDic.Count == 0)
                return null;

            int count = varDic.Count;
            MeterData retObj = new MeterData();
            retObj.MeterID = varDic["MeterID"][count - 1];
            retObj.MeterPlace = varDic["ProLine"][count - 1];
            retObj.RecordTime = DateTime.Parse(varDic["Time"][count - 1]);
            retObj.PeakUnitPrice = float.Parse(varDic["PeakUP"][count - 1]);
            retObj.GenUnitPrice = float.Parse(varDic["GenUP"][count - 1]);
            retObj.LowUnitPrice = float.Parse(varDic["LowUP"][count - 1]);
            retObj.PeakStartQuantity = float.Parse(varDic["PeakQut"][0]);
            retObj.PeakEndQuantity = float.Parse(varDic["PeakQut"][count - 1]);
            retObj.GenStartQuantity = float.Parse(varDic["GenQut"][0]);
            retObj.GenEndQuantity = float.Parse(varDic["GenQut"][count - 1]);
            retObj.LowStartQuantity = float.Parse(varDic["LowQut"][0]);
            retObj.LowEndQuantity = float.Parse(varDic["LowQut"][count - 1]);
            for (int i = 0; i < count; i++ )
            {
                float tmp1 = float.Parse(varDic["MaxCur"][i]);
                float tmp2 = float.Parse(varDic["MinCur"][i]);
                float tmp3 = float.Parse(varDic["MaxLoad"][i]);
                if (tmp1 > retObj.MaxCurrent)
                    retObj.MaxCurrent = tmp1;
                if (tmp2 > retObj.MinCurrent)
                    retObj.MinCurrent = tmp2;
                if (tmp3 > retObj.MaxLoad)
                    retObj.MaxLoad = tmp3;
            }
            return retObj;
        }
    }

    public class IDConvert
    {
        public readonly static string[] CodeType = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
        private readonly static string[] OrderTypes = { "MF","MO","MR","MS","MY","QM"};
        public static string OrderIDConvert(string type, string high, string mid, string low)
        {
            int charIndex = int.Parse(type);
            if (charIndex < 1 || charIndex > 6)
                charIndex = 2;
            string mt = OrderTypes[charIndex - 1];
            if (high.Length > 2)
                high = high.Substring(0, 2);
            int repeat = 2 - high.Length;   // 计算高位
            string id1 = "";
            while (repeat > 0)
            {
                id1 += "0";
                repeat--;
            }
            id1 += high;
            if (mid.Length > 4)
                mid = mid.Substring(0, 4);
            repeat = 4 - mid.Length; // 计算中位
            string id2 = "";
            while (repeat > 0)
            {
                id2 += "0";
                repeat--;
            }
            id2 += mid;
            if (low.Length > 4)
                low = low.Substring(0, 4);
            repeat = 4 - low.Length; // 计算低位
            string id3 = "";
            while (repeat > 0)
            {
                id3 += "0";
                repeat--;
            }
            id3 += low;

            return mt + id1 + id2 + id3;
        }

        public static string ProLineConvert(string pl1, string pl2)
        {
            // 计算车间类型
            int charIndex = int.Parse(pl2);
            if (charIndex < 1 || charIndex > 2)
                charIndex = 1;
            string type = CodeType[charIndex - 1];
            return type + pl1;
        }

        public static string BalIDConvert(string name1, string name2, string name3, string name4)
        {
            int repeat = 4 - name4.Length; // 计算高位
            string id1 = "";
            while (repeat > 0)
            {
                id1 += "0";
                repeat--;
            }
            id1 += name4;
            repeat = 4 - name3.Length; // 计算低位
            string id2 = "";
            while (repeat > 0)
            {
                id2 += "0";
                repeat--;
            }
            id2 += name3;
            repeat = 4 - name2.Length; // 计算低位
            string id3 = "";
            while (repeat > 0)
            {
                id3 += "0";
                repeat--;
            }
            id3 += name2;
            repeat = 4 - name1.Length; // 计算低位
            string id4 = "";
            while (repeat > 0)
            {
                id4 += "0";
                repeat--;
            }
            id4 += name1;
            return id1 + id2 + id3 + id4;
        }

        public static string NameIDConvert(string name1, string name2)
        {
            if (name1.Length > 4)
                name1 = name1.Substring(0, 4);
            int repeat = 4 - name1.Length; // 计算低位
            string id1 = "";
            while (repeat > 0)
            {
                id1 += "0";
                repeat--;
            }
            id1 += name1;
            return name2 + id1;
        }
    }

    public class WaitingOrder
    {
        public string ProductionLine { get; set; }
        public string ProLineCode { get; set; }
        public string OrderID { get; set; }
        public double Weight { get; set; }

        public static WaitingOrder ConvertDictionary2WaitingOrder(Dictionary<string, List<string>> varDic, string prolineCode)
        {
            if (varDic == null || varDic.Count == 0 || varDic["Time"].Count == 0)
                return null;

            string ProLine = IDConvert.ProLineConvert(varDic["ProLine1"][0], varDic["ProLine2"][0]);
            string WOrderNum1 = varDic["WOrderNum1"][0];
            string WOrderNum2 = varDic["WOrderNum2"][0];
            string WOrderNum3 = varDic["WOrderNum3"][0];
            string WOrderNum4 = varDic["WOrderNum4"][0];
            //double Wcount = double.Parse(varDic["Wcount"][latestIndex]);
            string orderID = IDConvert.OrderIDConvert(WOrderNum4, WOrderNum3, WOrderNum2, WOrderNum1);
            if (orderID == "MO0000000000")
                return null;
            WaitingOrder order = new WaitingOrder();
            order.OrderID = orderID;
            order.ProductionLine = ProLine;
            order.ProLineCode = prolineCode;
            //order.Weight = Wcount;
            return order;
        }

        public static WaitingOrder ConvertDictionary2WaitingOrder(Dictionary<string, List<string>> varDic, int listCount)
        {
            if (varDic == null || varDic.Count == 0 || varDic["Time"].Count == 0 || listCount == 0)
                return null;

            int latestIndex = listCount - 1;
            string ProLine = IDConvert.ProLineConvert(varDic["ProLine1"][latestIndex], varDic["ProLine2"][latestIndex]);
            string WOrderNum1 = varDic["WOrderNum1"][latestIndex];
            string WOrderNum2 = varDic["WOrderNum2"][latestIndex];
            string WOrderNum3 = varDic["WOrderNum3"][latestIndex];
            string WOrderNum4 = varDic["WOrderNum4"][latestIndex];
            //double Wcount = double.Parse(varDic["Wcount"][latestIndex]);
            string orderID = IDConvert.OrderIDConvert(WOrderNum4, WOrderNum3, WOrderNum2, WOrderNum1);
            if (orderID == "MO0000000000")
                return null;
            WaitingOrder order = new WaitingOrder();
            order.OrderID = orderID;
            order.ProductionLine = ProLine;
            //order.Weight = Wcount;
            return order;
        }
    }

    public class OrderFeedback
    {
        public string OrderID { get; set; }
        public double PowerRate { get; set; }
        public double PowerCount { get; set; }
        public double Efficiency { get; set; }
        public double RongZhi { get; set; }
        public List<TeamInfo> TeamList { get; set; }
        public string ProLineCode { get; set; }
        public string ProductionLine { get; set; }
        public DateTime StartTime { get; set; }
        public DateTime EndTime { get; set; }

        public static bool Compare(OrderFeedback order1, OrderFeedback order2)
        {
            if (order1.OrderID != order2.OrderID)
                return false;
            if (order1.PowerRate != order2.PowerRate)
                return false;
            if (order1.Efficiency != order2.Efficiency)
                return false;
            if (order1.RongZhi != order2.RongZhi)
                return false;
            if (order1.ProductionLine != order2.ProductionLine)
                return false;
            if (order1.StartTime != order2.StartTime)
                return false;
            if (order1.EndTime != order2.EndTime)
                return false;
            return true;
        }
    }

    public class TeamInfo
    {
         public string TeamID { get; set; }
        public string MixingerID { get; set; }
        public string ExtruderID { get; set; }
        public string PackerID { get; set; }
        public string QualityID { get; set; }
    }

    public class TeamWorkTime
    {
        public string ProductionLine { get; set; }
        public string ProLineCode { get; set; }
        public DateTime StartTime { get; set; }
        public DateTime EndTime { get; set; }
        public string TeamID { get; set; }
        public string MixingerID { get; set; }
        public string ExtruderID { get; set; }
        public string PackerID { get; set; }
        public string QualityID { get; set; }

        public static List<TeamWorkTime> Convert2TeamWorkTime(Dictionary<string, List<string>> dic, string proLineCode)
        {
            if (dic == null || dic.Count == 0 || dic["Time"].Count == 0)
                return null;
            
            List<TeamWorkTime> retList = new List<TeamWorkTime>();
            Dictionary<string, List<TeamWorkTime>> teamDic = new Dictionary<string, List<TeamWorkTime>>();
            int count = dic["Time"].Count;
            for (int i = 0; i < count; i++)
            {
                int index = int.Parse(dic["TeamName1"][i]);
                if (index < 1 || index > 26)
                    index = 1;
                TeamWorkTime team = new TeamWorkTime();
                team.TeamID = IDConvert.CodeType[index - 1] + "班";
                team.StartTime = DateTime.Parse(dic["Time"][i]);
                team.EndTime = team.StartTime;
                team.ProductionLine = IDConvert.ProLineConvert(dic["ProLine1"][i], dic["ProLine2"][i]);
                team.ProLineCode = proLineCode;
                team.MixingerID = IDConvert.NameIDConvert(dic["Mixinger1"][i], dic["Mixinger2"][i]);
                team.ExtruderID = IDConvert.NameIDConvert(dic["Extruder1"][i], dic["Extruder2"][i]);
                team.PackerID = IDConvert.NameIDConvert(dic["Packer1"][i], dic["Packer2"][i]);
                team.QualityID = IDConvert.NameIDConvert(dic["Qualer1"][i], dic["Qualer2"][i]);
                // 如果还是当前班组，持续更新结束时间
                if (teamDic.Keys.Contains(team.ProductionLine))
                {
                    int teamCount = teamDic[team.ProductionLine].Count - 1;
                    if (team.TeamID == teamDic[team.ProductionLine][teamCount].TeamID &&
                        team.MixingerID == teamDic[team.ProductionLine][teamCount].MixingerID &&
                        team.ExtruderID == teamDic[team.ProductionLine][teamCount].ExtruderID &&
                        team.PackerID == teamDic[team.ProductionLine][teamCount].PackerID &&
                        team.QualityID == teamDic[team.ProductionLine][teamCount].QualityID)
                    {
                        TimeSpan span = (TimeSpan)(team.StartTime - teamDic[team.ProductionLine][teamCount].EndTime);
                        if (span.TotalHours < 0.25)
                        {
                            teamDic[team.ProductionLine][teamCount].EndTime = team.EndTime;
                        }
                        else
                        {
                            teamDic[team.ProductionLine].Add(team);
                        }
                    }
                    else
                    {
                        teamDic[team.ProductionLine].Add(team);
                    }
                }
                else
                    teamDic.Add(team.ProductionLine, new List<TeamWorkTime>() { team });
            }

            foreach(KeyValuePair<string, List<TeamWorkTime>> item in teamDic)
            {
                retList.AddRange(item.Value);
            }

            return retList;
        }
        public static List<TeamWorkTime> Convert2TeamWorkTime(Dictionary<string, List<string>> dic, int listCount)
        {
            if (dic == null || dic.Count == 0 || dic["Time"].Count == 0 || listCount == 0)
                return null;
            string currTeamID = "";
            List<TeamWorkTime> retList = new List<TeamWorkTime>();
            TeamWorkTime teamobj = null;
            for(int i = 0; i < listCount; i++)
            {
                int index = int.Parse(dic["TeamName1"][i]);
                if (index < 1 || index > 26)
                    index = 1;
                string TeamID = IDConvert.CodeType[index - 1] + "班";
                // 如果还是当前班组，持续更新结束时间
                if (currTeamID == TeamID)
                {
                    if (teamobj != null)
                    {
                        teamobj.EndTime = DateTime.Parse(dic["Time"][i]);
                    }
                }
                // 如果有新的班组登陆
                else if(currTeamID != TeamID)
                {
                    // 保存上一次的班组数据
                    if (teamobj != null)
                    {
                        retList.Add(teamobj);
                    }
                    teamobj = new TeamWorkTime();
                    teamobj.StartTime = DateTime.Parse(dic["Time"][i]);
                    teamobj.EndTime = teamobj.StartTime;
                    teamobj.TeamID = TeamID;
                    teamobj.ProductionLine = IDConvert.ProLineConvert(dic["ProLine1"][i], dic["ProLine2"][i]);
                    teamobj.MixingerID = IDConvert.NameIDConvert(dic["Mixinger1"][i], dic["Mixinger2"][i]);
                    teamobj.ExtruderID = IDConvert.NameIDConvert(dic["Extruder1"][i], dic["Extruder2"][i]);
                    teamobj.PackerID = IDConvert.NameIDConvert(dic["Packer1"][i], dic["Packer2"][i]);
                    teamobj.QualityID = IDConvert.NameIDConvert(dic["Qualer1"][i], dic["Qualer2"][i]);
                    currTeamID = TeamID;
                }
            }

            if (teamobj != null)
                retList.Add(teamobj);

            return retList;
        }
    }

    public class HighLowInfo
    {
        public string DeviceID { get; set; }
        public DateTime Date { get; set; }
        public float MaxCurrent { get; set; }
        public float MinCurrent { get; set; }
        public float MaxVoltage { get; set; }
        public float MinVoltage { get; set; }
        public float MaxLoad { get; set; }
        public float MinLoad { get; set; }
        public float PowerFactor { get; set; }

        public static List<HighLowInfo> Convert2HighLowInfo(Dictionary<string, List<string>> dic, string deviceID)
        {
            if (dic == null || dic.Count == 0 || dic["Time"].Count == 0)
                return null;

            List<HighLowInfo> retList = new List<HighLowInfo>();
            int count = dic["Time"].Count;
            HighLowInfo info = null;
            // 新的一天
            DateTime newDay = DateTime.Parse(dic["Time"][0]).AddDays(1).Date;
            bool isUpdate = true;
            for (int i = 0; i < count; i++ )
            {
                DateTime date = DateTime.Parse(dic["Time"][i]);
                if(date >= newDay)
                {
                    newDay = newDay.AddDays(1);
                    retList.Add(info);
                    isUpdate = true;
                }
                float Ia = float.Parse(dic["Ia"][i]);
                float Ib = float.Parse(dic["Ib"][i]);
                float Ic = float.Parse(dic["Ic"][i]);
                float current = Ia + Ib + Ic;
                float Uab = float.Parse(dic["Uab"][i]);
                float Ubc = float.Parse(dic["Ubc"][i]);
                float Uca = float.Parse(dic["Uca"][i]);
                float voltage = (Uab + Ubc + Uca) / 3;
                float power = float.Parse(dic["power"][i]);
                float cos = float.Parse(dic["cos"][i]);
                float degree = float.Parse(dic["degree"][i]);
                if (isUpdate)
                {
                    info = new HighLowInfo();
                    info.DeviceID = deviceID;
                    info.Date = date;
                    info.MaxCurrent = current;
                    info.MinCurrent = info.MaxCurrent;
                    info.MaxVoltage = voltage;
                    info.MinVoltage = info.MaxVoltage;
                    info.MaxLoad = power;
                    info.PowerFactor = cos;
                    isUpdate = false;
                }
                else
                {
                    info.MaxCurrent = current > info.MaxCurrent ? current : info.MaxCurrent;
                    if (current > 0)
                        info.MinCurrent = current < info.MinCurrent ? current : info.MinCurrent;
                    info.MaxVoltage = voltage > info.MaxVoltage ? voltage : info.MaxVoltage;
                    if (voltage > 0)
                        info.MinVoltage = voltage < info.MinVoltage ? voltage : info.MinVoltage;
                    info.MaxLoad = power > info.MaxLoad ? power : info.MaxLoad;
                    if (power > 0)
                        info.MinLoad = power < info.MinLoad ? power : info.MinLoad;
                    info.PowerFactor = cos > info.PowerFactor ? cos : info.PowerFactor;
                }
            }
            if (info != null && isUpdate == false)
                retList.Add(info);
            return retList;
        }
    }

    public class HighLowVoltage
    {
        public string DeviceID { get; set; }
        public DateTime UpdateTime { get; set; }
        public float Degree { get; set; }

        private readonly static int Interval = 300;

        public static List<HighLowVoltage> Convert2HighLowVoltage(Dictionary<string, List<string>> dic, string deviceID)
        {
            if (dic == null || dic.Count == 0 || dic["Time"].Count == 0)
                return null;

            // 按照5分钟的间隔记录数据
            DateTime startTime = DateTime.Now;
            if (dic["Time"] != null && dic["Time"].Count > 0)
                startTime = DateTime.Parse(dic["Time"][0]).AddSeconds(Interval);
            List<HighLowVoltage> retList = new List<HighLowVoltage>();
            int count = dic["Time"].Count;
            HighLowVoltage info = null;
            bool isFinished = true;
            for (int i = 0; i < count; i++)
            {
                isFinished = false;
                info = new HighLowVoltage();
                info.DeviceID = deviceID;
                info.UpdateTime = DateTime.Parse(dic["Time"][i]);
                info.Degree = float.Parse(dic["degree"][i]);
                if (info.UpdateTime >= startTime)
                {
                    startTime = startTime.AddSeconds(Interval);
                    retList.Add(info);
                    isFinished = true;
                }
            }
            if (info != null && isFinished == false)
                retList.Add(info);
            return retList;
        }
    }

    public class HumidityTemperature
    {
        public DateTime Date { get; set; }
        public double Temperature { get; set; }
        public double Humidity { get; set; }

        public static List<HumidityTemperature> Convert2HumTemInfo(Dictionary<string, List<string>> dic)
        {
            if (dic == null || dic.Count == 0 || dic["Time"].Count == 0)
                return null;

            List<HumidityTemperature> retList = new List<HumidityTemperature>();
            int count = dic["Time"].Count;
            HumidityTemperature info = null;
            // 新的一天
            DateTime newDay = DateTime.Parse(dic["Time"][0]).AddDays(1).Date;
            for (int i = 0; i < count; i++)
            {
                DateTime date = DateTime.Parse(dic["Time"][i]).Date;
                if (date == newDay)
                {
                    newDay = newDay.AddDays(1).Date;
                    if (info != null)
                    {
                        retList.Add(info);
                        info = null;
                    }
                }
                float hum = float.Parse(dic["TEM_HUMHUM"][i]);
                float tem = float.Parse(dic["TEM_HUMTEM"][i]);
                if (info == null)
                {
                    info = new HumidityTemperature();
                    info.Date = date;
                    info.Humidity = hum;
                    info.Temperature = tem;
                }
                else
                {
                    info.Humidity = hum > info.Humidity ? hum : info.Humidity;
                    info.Temperature = tem > info.Temperature ? tem : info.Temperature;
                }
            }
            if (info != null)
                retList.Add(info);
            return retList;
        }
    }

    public class HSMLJInfo
    {
        public DateTime? StartTime { get; set; }
        public DateTime? EndTime { get; set; }
        public DateTime UpdateTime { get; set; }
        public float MainA { get; set; }
        public float MainB { get; set; }
        public float ExtraA { get; set; }
        public float ExtraB { get; set; }
        public float StartPower { get; set; }
        public float EndPower { get; set; }

        public static HSMLJInfo ConvertDictionary2HSMLJWithEndTime(Dictionary<string, string> dic)
        {
            if (dic == null || dic.Count == 0)
                return null;
            if (string.IsNullOrEmpty(dic["Time"]))
                return null;

            HSMLJInfo obj = new HSMLJInfo();
            obj.StartTime = null;
            obj.EndTime = DateTime.Parse(dic["Time"]);
            obj.UpdateTime = DateTime.Parse(dic["Time"]);
            obj.MainA = float.Parse(dic["HS_PLCMA_SV"]);
            obj.MainB = float.Parse(dic["HS_PLCMB_SV"]);
            obj.ExtraA = float.Parse(dic["HS_PLCASA_SV"]);
            obj.ExtraB = float.Parse(dic["HS_PLCASB_SV"]);
            obj.EndPower = float.Parse(dic["degree"]);

            return obj;
        }

        public static HSMLJInfo ConvertDictionary2HSMLJWithEndTime(Dictionary<string, List<string>> dic)
        {
            if (dic == null || dic["Time"] == null || dic["Time"].Count == 0)
                return null;
            int count = dic["Time"].Count - 1;
            HSMLJInfo obj = new HSMLJInfo();
            obj.StartTime = DateTime.Parse(dic["Time"][0]);
            obj.EndTime = DateTime.Parse(dic["Time"][count]);
            obj.UpdateTime = DateTime.Parse(dic["Time"][count]);
            obj.MainA = float.Parse(dic["HS_PLCMA_SV"][count]);
            obj.MainB = float.Parse(dic["HS_PLCMB_SV"][count]);
            obj.ExtraA = float.Parse(dic["HS_PLCASA_SV"][count]);
            obj.ExtraB = float.Parse(dic["HS_PLCASB_SV"][count]);
            obj.StartPower = float.Parse(dic["degree"][0]);
            obj.EndPower = float.Parse(dic["degree"][count]);
            return obj;
        }

        public static HSMLJInfo ConvertDictionary2HSMLJ(Dictionary<string, List<string>> dic)
        {
            if (dic == null || dic["Time"] == null || dic["Time"].Count == 0)
                return null;
            int count = dic["Time"].Count - 1;
            HSMLJInfo obj = new HSMLJInfo();
            obj.StartTime = DateTime.Parse(dic["Time"][0]);
            obj.EndTime = null;
            obj.UpdateTime = DateTime.Parse(dic["Time"][count]);
            obj.MainA = float.Parse(dic["HS_PLCMA_SV"][count]);
            obj.MainB = float.Parse(dic["HS_PLCMB_SV"][count]);
            obj.ExtraA = float.Parse(dic["HS_PLCASA_SV"][count]);
            obj.ExtraB = float.Parse(dic["HS_PLCASB_SV"][count]);
            obj.StartPower = float.Parse(dic["degree"][0]);
            obj.EndPower = float.Parse(dic["degree"][count]);
            return obj;
        }
    }
}
